<?php

namespace paper\dao;

use JetBrains\PhpStorm\Pure;

class Db extends Connection
{
    private Connection $connection;

    #[Pure]
    public function __construct($config = [])
    {
        parent::__construct($config);
        $this->connection = $this;
    }


    public function name($name): Query
    {
        return (new Query($this->connection))->name($name);
    }

    /**
     * @throws \Throwable
     */
    public function transaction(\Closure $closure)
    {
        try {
            $this->startTrans();
            $back = $closure();
            $this->commit();
            return $back;
        } catch (\Throwable $throwable) {
            $this->rollback();
            throw $throwable;
        }
    }
}